const fs = require('fs')
const path = require('path')
const { workerData, Worker } = require('worker_threads')

const arguments = process.argv.slice(3)
console.log(arguments)
if (arguments.length < 1) {
    console.log('请输入插件名称')
    process.exit(1)
}
if (fs.existsSync(path.join(__dirname, `./packages/${arguments[0]}/index.js`)) === false) {
    console.error('插件不存在，请手动在packages中添加插件')
    process.exit(1)
}

const options = {
    flow_id: '111',
    round_id: '222',
    chat_id: '333',
    node: {
        id: "1",
        moduleId: "1",
        nodeType: "1",
        name: "1",
        handles: [],
        formData: {},
    },
    nodes: [],
    edges: [],
    params: [{ input: "用户问的问题" }],
    formData: {},//节点配置，用户在页面上输入的配置
    sharedStorage: {},//共享存储
    plugin_info: {
        plugin_id: '',
        name: '',
        file_path: '',
        version: '',
        render_type: '',
        create_by: '1',
        options: {
            keepFiles: []
        }
    }
}
const worker = new Worker(path.join(__dirname, './thread.js'), {
    workerData: {
        pluginName: arguments[0],
        options
    },
    execArgv: ['--experimental-permission', '--allow-addons', '--allow-fs-write=*', '--allow-fs-read=*',]
})
worker.on('message', (data) => {
    console.log(data)
})
worker.on('error', (err) => {
    console.error(err)
})
worker.on('exit', (code) => {
    console.log('exit', code)
})

